 /*  
        Visualize 
        Explore 
     useR 
     musIc 
     lisTening 
  historY

  Notes:
  ======
  A 1BA7 project.
  
*/ 

String DBFILE = "dataset.csv";

int VISUAL_WIDTH = 800;
int VISUAL_HEIGHT = 400;
int ZBAR_HEIGHT = 10;
int VBAR_WIDTH = 10;
int HBAR_HEIGHT = 10;

int[] dates = new int[6];

// Widgets
ArrayList widgets;
CirclePacker packer;
AScrollBar vbar;
AScrollBar hbar;
ASlideBar zbar;
AButton bandsButton;
AButton usersButton;
AButton tracksButton;
ACheckBox chainCheckBox;
ASpinner levelSpinner;
ATextBox dateRangeBox;
AButton resetDateButton;

DataBase dataBase;
PFont defaultFont;
PImage top, bot;


DropList userList,trackList, albumList, bandList;
Scrollbar scrollBar;
boolean paused = false;

// listen for changes to widgets values
AWidgetListener listener;


int radius = 10, SCREENWIDTH = 800, SCREENHEIGHT = 600;

void setup() {
  size(SCREENWIDTH, SCREENHEIGHT);  
  defaultFont = loadFont("Tahoma-10.vlw");
  textFont(defaultFont);
  dataBase = new DataBase(DBFILE);
  dataBase.sort();
  userList = new DropList(1);
  albumList = new DropList(2);
  trackList = new DropList(3);
  bandList = new DropList(4);
  scrollBar = new Scrollbar();
  scrollBar.setDateRange(dataBase.getDateRange());
  background(255);
  top = loadImage("top.bmp");
  bot = loadImage("bottom.bmp");
  initWidgets();
  initListener();
  println(dataBase);

}

void initListener() {
  listener = new AWidgetListener() {
    public void valueChanged(AWidget widget) {
      if (widget == bandsButton) {
        packer.setData(dataBase.getBands());
      }
      else if (widget == usersButton) {
        packer.setData(dataBase.getUsers());
      }
      else if (widget == tracksButton) {
        packer.setData(dataBase.getTracks());        
      }
      else if (widget == chainCheckBox) {
        packer.setShowChain(chainCheckBox.getChecked());  
      }
      else if (widget == levelSpinner) {
        packer.setMaxLevel(levelSpinner.getValue());  
      }
      else if (widget == userList) {
        DataNode node = (DataNode) userList.getValue();
        packer.setData(node);
      }
      else if (widget == bandList) {
        DataNode node = (DataNode) bandList.getValue();
        packer.setData(node);
      }
      else if (widget == trackList) {
        DataNode node = (DataNode) trackList.getValue();
        packer.setData(node);
      }
      else if (widget == scrollBar) {
        println("scrollbar new daterange");
        DateRange range = scrollBar.getDateRange();
        println(range);
        println(range.getStart());
        println(range.getEnd());
        DataFilter filter = new DataFilter(range);
        packer.setFilter(filter);
        dateRangeBox.setValue(range.toString());
      }
      else if (widget == resetDateButton) {
        println("resetting");
        DateRange range = dataBase.getDateRange();
        scrollBar.setDateRange(range);
        dateRangeBox.setValue(range.toString());
        packer.setFilter(null);
      }
      else {
        println("???"+widget); 
      }
    }
  };

  // now link all widgets 
  bandsButton.setListener(listener);
  usersButton.setListener(listener);
  tracksButton.setListener(listener);
  chainCheckBox.setListener(listener);
  levelSpinner.setListener(listener);
  userList.setListener(listener);
  bandList.setListener(listener);
  trackList.setListener(listener);
  scrollBar.setListener(listener);
  resetDateButton.setListener(listener);
}


void initWidgets() {
  widgets = new ArrayList();
  //defaultFont = loadFont("Tahoma-10.vlw");
  defaultFont = createFont("Tahoma",10);
  textFont(defaultFont);
  
  packer = new CirclePacker();
  
  //colorMode(HSB, 360,100,100);
  colorMode(HSB,255);
  //colorMode(RGB);
  
  //DataNode node = dataBase.getBand("Arctic Monkeys");
  packer.setBounds(0, 70, VISUAL_WIDTH-VBAR_WIDTH, VISUAL_HEIGHT-HBAR_HEIGHT-ZBAR_HEIGHT);
  
  vbar = new AScrollBar(true);
  hbar = new AScrollBar(false);
  zbar = new ASlideBar(false);
 
  packer.setVertBar(vbar);
  packer.setHorizBar(hbar);
  packer.setZoomBar(zbar);
  
  widgets.add(vbar);
  widgets.add(hbar);
  widgets.add(zbar);
  widgets.add(packer);

  // layout manager
  int bx = 10;
  int by = packer.getY() + VISUAL_HEIGHT + 10;
  int bw = 60;
  int bh = 14;
  
  bandsButton = new AButton(bx, by, bw, bh, "Bands"); 
  usersButton = new AButton(bx+bw+10, by, bw, bh, "Users"); 
  tracksButton = new AButton(bx+(bw+10)*2, by, bw, bh, "Tracks"); 
  chainCheckBox = new ACheckBox(bx+(bw+10)*3, by, bh, "Chains");
  
  levelSpinner = new ASpinner(bx+(bw+10)*5, by, 50, bh, "Node Level:");
  levelSpinner.setValues(0,10,0);
  
  dateRangeBox = new ATextBox(bx+(bw+10)*7, by, 180, bh,"Date Range:");
  dateRangeBox.setValue(dataBase.getDateRange().toString());

  resetDateButton = new AButton(bx+(bw+10)*8+120, by, bw+10, bh, "Reset Dates"); 
  
  widgets.add(bandsButton);
  widgets.add(usersButton);
  widgets.add(tracksButton);
  widgets.add(chainCheckBox);
  widgets.add(levelSpinner);
  widgets.add(dateRangeBox);
  widgets.add(resetDateButton);

}

void drawWidgets() {
  for (int i=0; i < widgets.size(); i++) {
    AWidget widget = (AWidget) widgets.get(i);
    widget.update();
    widget.draw();  
  }  
  

}

void draw() {
   
  background(255);
  drawWidgets();

  fill(#f2f2f2);         //Simple menu bar
  stroke(0);
  userList.draw();
  bandList.draw();
  trackList.draw();
  scrollBar.draw();
  image(top,width-10,60);
  image(bot,width-10,450);
  
  vbar.setFocus(isFocus());
  hbar.setFocus(isFocus());
  zbar.setFocus(isFocus());
  bandsButton.setFocus(isFocus());
  usersButton.setFocus(isFocus());
  tracksButton.setFocus(isFocus());
  chainCheckBox.setFocus(isFocus());
  levelSpinner.setFocus(isFocus());
  resetDateButton.setFocus(isFocus());
  //dates = scrollBar.getDateRange();
  //println("Current Start Date " + dates[0] + "th of the " + dates[1] + "th " + dates[2]);
  //println("Current End Date " + dates[3] + "th of the " + dates[4] + "th " + dates[5]);

}



boolean isFocus(){
  return userList.getFocus() && bandList.getFocus() && albumList.getFocus() && trackList.getFocus() && scrollBar.getFocus();
}

void doUpdates() {
/*
  // do the focus here
  int updates = 0;
  for (int i=0; i < listboxs.size(); i++) {
    AListBox listbox = (AListBox) listboxs.get(i);  
    if (listbox.update(mouseX, mouseY)) {
      updates++;      
    }
  }
  if (updates > 0) {
    redraw();
  }
*/


}

/*
void mouseMoved() {
  // allow mouse of component which focus
  AWidget widget;
  for (int i=0; i < widgets.size(); i++) {
    widget = (AWidget) widgets.get(i);
    widget.mouseOver(mouseX, mouseY);
  }
}
*/

void mousePressed() {
  // do the focus here
  AWidget widget;
  for (int i=0; i < widgets.size(); i++) {
    widget = (AWidget) widgets.get(i);
    widget.mousePress();
  }
}

void mouseReleased() {
  AWidget widget;
  for (int i=0; i < widgets.size(); i++) {
    widget = (AWidget) widgets.get(i);
    widget.mouseRelease();
  }
}

void doPause() {
  println("Paused");
  paused = true;   
  noLoop();
}

void doResume() {
  println("Resuming");
  paused = false;    
  loop();
}


void keyPressed() {
  if (keyCode == 32) {
    if (paused) {
      doResume();
    }
    else {
      doPause();
    } 
  }
}


